<!doctype html>

<head>
<style>
    body {
        font-family: sans-serif;
    }

    table {
        border-collapse: collapse;
    }

    td, th {
        padding: 5px;
        font-size: 12px;
        font-weight: normal;
    }

    th {
        background: #cfcfcf;
    }

</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"
    ></script>
<script>
    var tests = {
        put1: 'write first key/value pair',
        put100: 'write first 100 key/value pairs',
        putfull: 'write full DB.',
        final1: 'finalize a writer with first item',
        final100: 'finalize a writer with first 100 items',
        finalfull: 'finalize a writer with the full DB',
        open1: 'open a reader with the first item',
        open100: 'open a reader with first 100 items',
        openfull: 'open a reader with the full DB',
        randget1: 'get a random key from a reader',
        repeatget1: 'get the same key repeatedly from a reader',
        repeatget100: 'get the same 100 keys repeatedly from a reader',
        iteritems: 'call iteritems on reader with entire DB'
    };

    var test_order = ['put1', 'put100', 'putfull', 'final1', 'final100',
                      'finalfull', 'open1', 'open100', 'openfull', 'randget1',
                      'repeatget1', 'repeatget100', 'iteritems'];

    var queue = [];
    var running = 0;

    function enqueue(test, db, hash, cb)
    {
        function done(result)
        {
            running--;
            cb(result);
            maybe_more();
        }

        queue.push([ test, db, hash, done ]);
        maybe_more();
    }

    function maybe_more()
    {
        while(running <= 5 && queue.length)
        {
            var item = queue.pop();
            var url = ('/perf?test=' + item[0]
                        + '&db=' + item[1]
                        + '&hash=' + item[2]);
            running++;
            $('<p>').text(url).appendTo('body');
            $.get(url, null, item[3]);
        }
    }

    var dbs = {
        pwdump: "3000 MD5s + passwords",
        top250pws: "250 most common web passwords and their rank"
    };

    var hashes = {
        djb: 'djb_hash()',
        py: 'hash()'
    };

    function start()
    {
        var tr = $('thead tr');
        for(var db in dbs)
        {
            for(var hash in hashes)
            {
                ($('<th>')
                    .text(db + ' ' + hash)
                    .attr('title', dbs[db] + ' || ' + hashes[hash])
                    .appendTo(tr));
            }
        }

        var tbody = $('tbody');

        for(var i = 0; i < test_order.length; i++)
        {
            var test = test_order[i];

            var tr = $('<tr>').appendTo(tbody);

            ($('<td>')
                .text(test)
                .attr('title', 'Time to ' + tests[test])
                .appendTo(tr));

            function enqueue2(test, db, hash, td)
            {
                enqueue(test, db, hash, function(result)
                {
                    td.text(result);
                });
            }

            for(var db in dbs)
            {
                for(var hash in hashes)
                {
                    var id = db+hash+test;
                    var td = ($('<td>')
                        .text('-')
                        .appendTo(tr));

                    enqueue2(test, db, hash, td);
                }
            }
        }
    }
    $(start);
</script>

<body>

<table border="1">
<thead>
<tr>
    <th>&nbsp;
<tbody>
</table>
